package demo.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import demo.dao.ManagerDao;
import demo.dao.ReaderDao;
import demo.entity.Manager;
import demo.entity.Reader;

public class ManagerEnter extends JFrame implements ActionListener {
	private JFrame frame = new JFrame("图书馆管理系统 ");
	private JPanel ReaderEnterPane;
	private ImageIcon background;
	private JPanel managerEnterPane;
	private JTextField readerNametextField;
	private JPasswordField readerPasswordtextField;
	public static String name,password;
	public static int number;
	Manager loginManager;


	public ManagerEnter() 
	{
		 background = new ImageIcon(this.getClass().getResource("background.jpg"));
		 JLabel label = new JLabel(background);
		 label.setBounds(0, 0, background.getIconWidth(),background.getIconHeight());
		
		 managerEnterPane = (JPanel) frame.getContentPane(); 
		 //getContentPane().setLayout(new BoxLayout(getContentPane(), BoxLayout.Y_AXIS));
		 managerEnterPane.setOpaque(false);
		 managerEnterPane.setLayout(null);
		 frame.getLayeredPane().setLayout(null);
		 
		 frame.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE)); 

		 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		 frame.setBounds(500, 200, 450, 300);
		 frame.setResizable(false);
		 frame.setVisible(true);

		
		 /*JPanel managerEnterPane=new JPanel();
		 managerEnterPane.setLayout(null);//无布局
		 frame.setLayout(new BorderLayout());
		 frame.add(managerEnterPane,BorderLayout.CENTER);*/
		
		
		 JLabel ReaderNameLabel = new JLabel("用户名:");
		 managerEnterPane.add(ReaderNameLabel);
		 ReaderNameLabel.setBounds(80, 37, 101, 37);
		 ReaderNameLabel.setFont(new Font("",Font.BOLD,20));
		 ReaderNameLabel.setForeground(Color.white);
		
		 JLabel ReaderPasswordLabel = new JLabel("密码:");
		 managerEnterPane.add(ReaderPasswordLabel);
		 ReaderPasswordLabel.setBounds(95, 121, 61, 24);
		 ReaderPasswordLabel.setFont(new Font("",Font.BOLD,20));
		 ReaderPasswordLabel.setForeground(Color.white);
		
		 readerNametextField = new JTextField();
		 managerEnterPane.add(readerNametextField);
		 readerNametextField.setBounds(156, 39, 178, 37);		
		 readerNametextField.setColumns(10);
		
		 readerPasswordtextField = new JPasswordField();
		 managerEnterPane.add(readerPasswordtextField);
		 readerPasswordtextField.setForeground(Color.BLACK);
		 readerPasswordtextField.setBounds(156, 117, 178, 37);		
		 readerPasswordtextField.setColumns(10);
		
		 JButton returnButton = new JButton("↩");
		 managerEnterPane.add(returnButton);
		 returnButton.setBounds(10, 5, 61,25);
		 returnButton.setFont(new Font("↩",Font.BOLD,17));
		 returnButton.addActionListener(this);
		 
		 JButton registerButton = new JButton("取消");
		 managerEnterPane.add(registerButton);
		 registerButton.setBounds(246, 187, 88, 37);
		 registerButton.setFont(new Font("",Font.BOLD,17));
		 registerButton.addActionListener(this);
		
		
		 JLabel ForgetLabel = new JLabel("忘记密码？");
		 managerEnterPane.add(ForgetLabel);
		 ForgetLabel.setBounds(358, 123, 66, 40);
		 ForgetLabel.setForeground(Color.blue);
		
		
		 JButton LandButton = new JButton("登录");
		 managerEnterPane.add(LandButton);
		 LandButton.setFont(new Font("", Font.BOLD, 17));
		 LandButton.setBounds(96, 187, 88, 37);
		
		

		 frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		 
			returnButton.addActionListener(new ActionListener(){
				@Override
				public void actionPerformed(ActionEvent e){
				
					// TODO Auto-generated method stub	
					frame.dispose();
					new Welcome();
				}			
			});
			
			LandButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if (e.getSource()==LandButton) {
						Manager manager = authenticate(readerNametextField.getText().trim(), new String(readerPasswordtextField.getPassword()).trim());
						if (manager != null) {
							loginManager = manager;
							frame.dispose();
							EventQueue.invokeLater(new Runnable() {
								public void run() {
									try {
										ManagerFrame managerFrame = new ManagerFrame();
										managerFrame.setVisible(true);
									} catch (Exception e) {
										e.printStackTrace();
									}
								}
							});
							
						} else
							JOptionPane.showMessageDialog(null,"账号或密码错误！","账号或密码错误", JOptionPane.WARNING_MESSAGE);
					}
					
				}
				
					public Manager authenticate(String id, String password) {
		  	        String passwordMD5 = password;
			        ManagerDao managerDao = new ManagerDao();
			        Manager manager = managerDao.findById(id);
			        if (manager != null && manager.getPassword().equals(passwordMD5)) {
				        return manager;
			    }
			        else
				        return null;
		        }
		 
		           public Manager login() {
			           //this.setVisible(true);
			           return loginManager;
		         }
			
			});
			registerButton.addActionListener(new ActionListener() {
		    	public void actionPerformed(ActionEvent e) {
		    		frame.dispose();
		    	}
		    	});
}


	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO 自动生成的方法存根
		
	}
}
	
	

